<?php
$AFTtrees = AFT_TreeTable();	
$AFTsettings = AFT_load('settings');
$tdb = AFT_TreeIndex($AFTtrees);
?>
<div class="settings">
	<input type="checkbox" class="saveConf" name="settings-hideBreedingOrchards" <?=$AFTsettings['hideBreedingOrchards']?" checked=\"checked\"":""?>> Hide orchards/groves with exactly 1 mastered tree (breeders)</br>
	<hr>
	<input type="checkbox" class="saveConf" name="settings-removeUnmasteredFromOrchards" <?=$AFTsettings['removeUnmasteredFromOrchards']?" checked=\"checked\"":""?>> Remove unmastered and spare/forced/count trees from orchards/groves</br>
	<input type="checkbox" class="saveConf" name="settings-removeAllMastered" <?=$AFTsettings['removeAllMastered']?" checked=\"checked\"":""?>> Remove all mastered trees from orchards/groves</br>
	<input type="checkbox" class="saveConf" name="settings-removeIllegalItems" <?=$AFTsettings['removeIllegalItems']?" checked=\"checked\"":""?>> Remove illegal objects from orchards/groves</br>
	<hr>
	<input type="checkbox" class="saveConf" name="settings-populateOrchards" <?=$AFTsettings['populateOrchards']?" checked=\"checked\"":""?>> Populate empty orchards/groves with 1 tree type:
		Orchards:&nbsp;<select class="saveConf" name="settings-populateWith" style="font-size:10px;">
			<option value="" <?=$AFTsettings['populateWith']==""?" selected":""?> >none</option>
			<option value="anyMastered" <?=$AFTsettings['populateWith']=="anyMastered"?" selected":""?>>any mastered tree</option>
			<option value="lowestMastery" <?=$AFTsettings['populateWith']=="lowestMastery"?" selected":""?>>lowest mastery count</option>
			<option value="highestMastery" <?=$AFTsettings['populateWith']=="highestMastery"?" selected":""?>>highest mastery count</option>
			<optgroup label="Specific Tree">
<?php
	foreach ($AFTtrees as $code => $t) {
		if ($code == $t['name'] || $t['cnt'] < 1) continue;
		
		echo "<option value=\"tree-$code\"".($AFTsettings['populateWith'] == "tree-$code"?" selected":"").">{$t['realname']}</option>\n";
	}
?>
			</optgroup>
		</select></br>
		Super&nbsp;Orchards:&nbsp;<select class="saveConf" name="settings-populateSuperWith" style="font-size:10px;">
			<option value="" <?=$AFTsettings['populateSuperWith']==""?" selected":""?> >none</option>
			<option value="anyMastered" <?=$AFTsettings['populateSuperWith']=="anyMastered"?" selected":""?>>any mastered tree</option>
			<option value="lowestMastery" <?=$AFTsettings['populateSuperWith']=="lowestMastery"?" selected":""?>>lowest mastery count</option>
			<option value="highestMastery" <?=$AFTsettings['populateSuperWith']=="highestMastery"?" selected":""?>>highest mastery count</option>
			<optgroup label="Specific Tree">
<?php
	foreach ($AFTtrees as $code => $t) {
		if ($code == $t['name'] || $t['cnt'] < 1) continue;
		
		echo "<option value=\"tree-$code\"".($AFTsettings['populateSuperWith'] == "tree-$code"?" selected":"").">{$t['realname']}</option>\n";
	}
?>
			</optgroup>
		</select></br>
		Groves:&nbsp;<select class="saveConf" name="settings-populateGroveWith" style="font-size:10px;">
			<option value="" <?=$AFTsettings['populateGroveWith']==""?" selected":""?> >none</option>
			<option value="anyMastered" <?=$AFTsettings['populateGroveWith']=="anyMastered"?" selected":""?>>any mastered tree</option>
			<option value="lowestMastery" <?=$AFTsettings['populateGroveWith']=="lowestMastery"?" selected":""?>>lowest mastery count</option>
			<option value="highestMastery" <?=$AFTsettings['populateGroveWith']=="highestMastery"?" selected":""?>>highest mastery count</option>
			<optgroup label="Specific Tree">
<?php
	foreach ($AFTtrees as $code => $t) {
		if ($code == $t['name'] || $t['cnt'] < 1) continue;
		
		echo "<option value=\"tree-$code\"".($AFTsettings['populateGroveWith'] == "tree-$code"?" selected":"").">{$t['realname']}</option>\n";
	}
?>
			</optgroup>
		</select></br>
	<hr>
	<input style="width:100%;" type="button" value="Clear All Move Actions" onclick="actionconf('faction=general&actn=clearallorc')"></br>
	<input style="width:100%;" type="button" value="Clear All Remove Actions" onclick="actionconf('faction=general&actn=clearallremorc')"></br>
	<input style="width:100%;" type="button" value="Make All Free" onclick="actionconf('faction=general&actn=orcfree')"></br>
</div>

<?php

global $vWorldTime, $vServertime;
// if(!isset($vServertime)) $vServertime=trim(file_get_contents(F('servertime.txt')));
// echo "worldtime: ".date('Y-m-d H:i:s', $vWorldTime);
// echo "<br>servertime: ".date('Y-m-d H:i:s', $vServertime);

?>

<div class="maintable">
<table class="grid sortable">
	<caption>Orchards/Groves</caption>
	<thead>
		<tr>
			<th>Id</th>
			<th>Type</th>
			<th>Trees</th>
			<th>Capacity</th>
			<th>Progress</th>
			<th>Content</th>
		</tr>
	</thead>
	<tbody>
<?php
$objects  = @unserialize(file_get_contents(F('objects.txt')));
$vOrchards = AFT_load('orchards');
$numOrchards = 0;
$numCapa = 0;
$numTrees = 0;
foreach ($objects as $oID => $o) {
	if ($o['className']=='OrchardFeatureBuilding' 
		|| $o['className']=='OrchardBuilding' 
		|| $o['className'] == "ArboretumBuilding") {
		$numOrchards++;
		// $rows = count($o['contents']);
		$numTT = 0;
		foreach ($o['contents'] as $conts) {
			$numTT += $conts['numItem'];
		}
		$numTrees += $numTT;
		$class = "orchard";

		$capa = AFT_capa($o);

		if ($o['className'] == "ArboretumBuilding") $class = "grove";
		if (stristr($o['itemName'],"superorchard")) $class = "superorchard";

		$numCapa += $capa;
		
		if ($numTT == 1 && $AFTsettings['hideBreedingOrchards']) {
// check if contained tree is mastered!
			$c = $o['contents'][0]['itemCode'];
			$t = $AFTtrees[$c];
			if ($t['mastery'] >= 1) {
				$hidden++;
				continue;
			}
		}
		
		echo '<tr class="'.$class.'">';
		echo '<td style="vertical-align: top;">'.$o['id'].'</td>';
		echo '<td style="vertical-align: top;">'.$class.'</td>';
		echo '<td style="vertical-align: top;">'.$numTT.'</td>';
		echo '<td style="vertical-align: top;">'.$capa.'</td>';
		echo '<td style="vertical-align: top;">'.AFT_OrcProgress($o).'</td>';
		// echo '<td style="vertical-align: top;">'.$percent.'</td>';
		echo '<td style="vertical-align: top;">';
/* 		echo '<pre>';
		print_r($o);
		echo '</pre>'; */
		if ($numTT) {
			echo '<table class="inner grid">';
			foreach ($o['contents'] as $conts) {
				echo '<tr>';
				echo AFT_tr($AFTtrees[$conts['itemCode']],array('icon','nameF','mastery'));

				$vrcnt=$conts['numItem'];
				echo '<td width="150">Remove 
				 <input name="oid" type="hidden" value="'.$o['id'].'" />
				 <input name="tid" type="hidden" value="'.$conts['itemCode'].'" />';
				 
				$cnt = 0;
				 foreach  ( $vOrchards['oid'] as $i => $or) {
					if ($or==$o['id'] && $vOrchards['tid'][$i]==$conts['itemCode']) {
						$cnt = $vOrchards['cnt'][$i];
					}
				}
		
?>			 
 <input maxlength="2" name="treecount" size="2" type="text" value="<?=$cnt?>" 
  onchange="fAction('faction=setorc&oid=<?=$o['id']?>&tid=<?=$conts['itemCode']?>&cnt=',this.value)" /> of <?=$conts['numItem']?></td>
<?php			 
				echo '</tr>';
			}
			echo '</table>';
		} else {
			echo 'empty';
		}
		echo '</td></tr>';
	}	
}
?>
	</tbody>
	<tfoot>
<?php if ($hidden) {
		echo "<tr><th colspan=\"6\">$hidden orchards/groves hidden</th></tr>";
	}
?>
	<tr>
		<td colspan="2">Total</td>
		<td><?=$numTrees?></td>
		<td><?=$numCapa?></td>
		<td><?=$numOrchards?> orchards/groves</td>
		<td></td>
	</tr>
	</tfoot>
</table>
<input name="faction2" type="hidden" value="orc_settings" />
</div>
